﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment2_OrderSystem
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    LoadDropDownLists();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        private void LoadDropDownLists()
        {
            try
            {
                ddlSQ.Items.Insert(0, new ListItem("-- Select --", "0"));
                ddlSQ.Items.Insert(1, new ListItem("What is the name of your first teacher?", "1"));
                ddlSQ.Items.Insert(2, new ListItem("What is the name of your grandpa?", "2"));
                ddlSQ.Items.Insert(3, new ListItem("Where your father was born?", "3"));
                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("~/App_Data/countries.xml"));
                ddlCountries.DataSource = ds;
                ddlCountries.DataTextField = "Name";
                ddlCountries.DataValueField = "ID";
                ddlCountries.DataBind();
                ddlCountries.Items.Insert(0, new ListItem("-- Select --", "0"));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                string sUsername = txtUsername.Value;
                string sEmail = txtEmail.Value;
                if (SQLAdapter.CheckIfUsernameExists(sUsername) == -1 && SQLAdapter.CheckIfUsernameExists(sEmail) == -1)
                {
                    // Add user to the system
                    User user = CreateUser();
                    string sClientID = SQLAdapter.InsertUser(user);
                    // Add cookies
                    AddCookies(sClientID, sUsername);
                    // Redirect to the home page
                    string sPath = "Home.aspx?NewUser=true&Username=" + sUsername + "&ClientID=" + sClientID;
                    Response.Redirect(sPath, false);
                }
                else
                {
                    existsErrorMessage.Attributes.CssStyle.Value = "display: inline";
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        private User CreateUser()
        {
            User user = new User();
            user.m_sUsername = txtUsername.Value;
            user.m_sPassword = txtPass.Value;
            user.m_sFirstname = txtFirstName.Value;
            user.m_sLastname = txtLastName.Value;
            user.m_sAddress = txtAddress.Value;
            user.m_sCity = txtCity.Value;
            user.m_sCountry = ddlCountries.SelectedItem.Text;
            user.m_sPhone = txtPhone.Value;
            user.m_sCellular = txtCellular.Value;
            user.m_sMail = txtEmail.Value;
            user.m_sSQ = ddlSQ.SelectedItem.Text;
            user.m_sAns = txtSA.Value;
            user.m_sCreditCard = txtCC.Value;
            user.m_lCategories = new List<int>();
            if (cbAndroid.Checked == true)
            {
                user.m_lCategories.Add(Convert.ToInt16(cbAndroid.Value));
            }
            if (cbApple.Checked == true)
            {
                user.m_lCategories.Add(Convert.ToInt16(cbApple.Value));
            }
            if (cbBlackbarry.Checked == true)
            {
                user.m_lCategories.Add(Convert.ToInt16(cbBlackbarry.Value));
            }
            if (cbWindowsPhone.Checked == true)
            {
                user.m_lCategories.Add(Convert.ToInt16(cbWindowsPhone.Value));
            }
            user.m_iIsAdmin = 0;
            return user;
        }
        private void AddCookies(string sClientID, string sUsername)
        {
            // Add cookies
            HttpCookie TimeCookie = new HttpCookie("LastEntered", DateTime.Now.ToString());
            HttpCookie IDCookie = new HttpCookie("ClientID", sClientID);
            HttpCookie UserCookie = new HttpCookie("Username", sUsername);
            TimeCookie.Expires = DateTime.Now.AddDays(100);
            IDCookie.Expires = DateTime.Now.AddDays(100);
            UserCookie.Expires = DateTime.Now.AddDays(100);
            Response.Cookies.Add(IDCookie);
            Response.Cookies.Add(UserCookie);
            Response.Cookies.Add(TimeCookie);
        }
    }
}